package com.cy.pj.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Aspect
public class PracticeCache {

    private Map<Object,Object> cache = new ConcurrentHashMap<>();

    @Pointcut("bean(*ServiceImpl)")
    private void doLog(){}

    @Around("doLog()")
    public Object doCacheAround(ProceedingJoinPoint joinPoint)throws Throwable{

        Object result = cache.get("aaa");
        if (result!=null)return result;

        long t1 = System.currentTimeMillis();
        result = joinPoint.proceed();
        long t2 = System.currentTimeMillis();
        System.out.println("time="+(t2-t1));

        cache.put("aaa", result);
        return result;
    }
}
